package com.shop2cn.iapi.agentproducts.testcase;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.BatchOffShelfOrDeleteProductBean;
import com.shop2cn.iapi.agentproducts.parameter.ImportAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.ProductListDto;
import com.shop2cn.iapi.agentproducts.service.BatchOffShelfOrDeleteProductCall;
import com.ymatou.iapi.godmch.parameter.createDepotBean;
import com.ymatou.mq.admin.parameter.MessageListBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.MqQueryCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.godmch.mch_depot_info;
/**
*批量下架或者移出商品Test
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_BatchOffShelfOrDeleteProduct{
	private static BatchOffShelfOrDeleteProductBean batchoffshelfordeleteproductBean;
	private static BatchOffShelfOrDeleteProductCall batchoffshelfordeleteproductCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("批量下架或者移出商品Test");
	}

	@Before
	public void caseUp() {
		batchoffshelfordeleteproductBean=new BatchOffShelfOrDeleteProductBean();
		batchoffshelfordeleteproductCall=new BatchOffShelfOrDeleteProductCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(batchoffshelfordeleteproductCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	
	public void verifyMq(List<String> pids, int UserId) throws Exception {
		Thread.sleep(5000);
		pids.forEach(pid -> {
			MessageListBean MessageListBean2 = MqQueryCallService.initoffsale_or_delete(pid);
			List<Map> joa2;
			try {
				joa2 = MqQueryCallService.GetMqMessage(MessageListBean2);
				Logger.debug(joa2);
				int redo = 0;
				while (joa2.size() < 2 && redo < 3) {
					Thread.sleep(5000);
					joa2 = MqQueryCallService.GetMqMessage(MessageListBean2);
					redo++;
				}
				
				Logger.verifyEquals(pid, joa2.get(0).get("productId"), "验证ProductId");
				Logger.verifyEquals(UserId, joa2.get(0).get("userId"), "验证AppId");
			} catch (Exception e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		});
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchOffShelfOrDeleteProduct_001() {
		Logger.start(true,"商户下架商品，代理商跟着移除.使用productid+userid。其他字段会忽略");
		try {
			Integer MchId = GodmchManageCallService.CreateGodMch();
            Integer agenttype=GodmchManageCallService.getAgentType(MchId, 1);
            Integer AgentId = GodmchManageCallService.getAgentMchId(MchId, agenttype);
            //添加导入商品
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(MchId, "", true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ImportAgentProductBean initImportAgentProduct = AddAgentProductCallService.initImportAgentProduct(AgentId, "", productId, MchId, false);
			String AgentProductId = AddAgentProductCallService.ImportAgentProduct(initImportAgentProduct);
			
			 //添加导入商品
			AddAgentProductBean addAgentProductBean2 = AddAgentProductCallService.initAddAgentProduct(MchId, "", true);
			String productId2 = AddAgentProductCallService.AddAgentProduct(addAgentProductBean2);
			ImportAgentProductBean initImportAgentProduct2 = AddAgentProductCallService.initImportAgentProduct(AgentId, "", productId2, MchId, false);
			String AgentProductId2 = AddAgentProductCallService.ImportAgentProduct(initImportAgentProduct2);
			
			
			batchoffshelfordeleteproductBean.setUserId(MchId);
			batchoffshelfordeleteproductBean.setProductIdList(Arrays.asList(productId,productId2));
			batchoffshelfordeleteproductBean.setAgentType(1);
			batchoffshelfordeleteproductBean.setBrandIdList(Arrays.asList(123,154));
			batchoffshelfordeleteproductBean.setCategoryIdList(Arrays.asList(1,2,3,5));
			batchoffshelfordeleteproductBean.setDepotIdList(Arrays.asList(4,71,4));
			batchoffshelfordeleteproductBean.setStatus(1);
			batchoffshelfordeleteproductBean.setTitle("1133454");
			batchoffshelfordeleteproductCall.setData(batchoffshelfordeleteproductBean);
			batchoffshelfordeleteproductCall.callService();			
			Logger.verifyEquals("200", batchoffshelfordeleteproductCall.getString("code"), "验证code");
			verifyMq(Arrays.asList(productId,productId2), MchId);
			
			
			List<ProductListDto> getProductLists = AddAgentProductCallService.GetProductLists(MchId);
			Logger.verifyEquals(0, getProductLists.size(), "验证上架商品");
			List<ProductListDto> getProductLists1 = AddAgentProductCallService.GetProductLists(AgentId);
			Logger.verifyEquals(0, getProductLists1.size(), "验证上架商品");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchOffShelfOrDeleteProduct_002() {
		Logger.start(true,"商户下架商品，代理商跟着移除.使用商品标题筛选");
		try {
			Integer MchId = GodmchManageCallService.CreateGodMch();
            Integer agenttype=GodmchManageCallService.getAgentType(MchId, 1);
            Integer AgentId = GodmchManageCallService.getAgentMchId(MchId, agenttype);
            //添加导入商品
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(MchId, "", true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ImportAgentProductBean initImportAgentProduct = AddAgentProductCallService.initImportAgentProduct(AgentId, "", productId, MchId, false);
			String AgentProductId = AddAgentProductCallService.ImportAgentProduct(initImportAgentProduct);
			
			 //添加导入商品
			AddAgentProductBean addAgentProductBean2 = AddAgentProductCallService.initAddAgentProduct(MchId, "", true);
			addAgentProductBean2.setBrandId(21489);
			String productId2 = AddAgentProductCallService.AddAgentProduct(addAgentProductBean2);
			ImportAgentProductBean initImportAgentProduct2 = AddAgentProductCallService.initImportAgentProduct(AgentId, "", productId2, MchId, false);
			String AgentProductId2 = AddAgentProductCallService.ImportAgentProduct(initImportAgentProduct2);
			
			batchoffshelfordeleteproductBean.setUserId(MchId);
			batchoffshelfordeleteproductBean.setBrandIdList(Arrays.asList(addAgentProductBean.getBrandId()));
			batchoffshelfordeleteproductCall.setData(batchoffshelfordeleteproductBean);
			batchoffshelfordeleteproductCall.callService();			
			Logger.verifyEquals("200", batchoffshelfordeleteproductCall.getString("code"), "验证code");
			verifyMq(Arrays.asList(productId), MchId);
			
			
			List<ProductListDto> getProductLists = AddAgentProductCallService.GetProductLists(MchId);
			Logger.verifyEquals(1, getProductLists.size(), "验证上架商品");
			List<ProductListDto> getProductLists1 = AddAgentProductCallService.GetProductLists(AgentId);
			Logger.verifyEquals(1, getProductLists1.size(), "验证上架商品");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchOffShelfOrDeleteProduct_003() {
		Logger.start(true,"商户下架商品，代理商跟着移除.使用商品类目筛选");
		try {
			Integer MchId = GodmchManageCallService.CreateGodMch();
            Integer agenttype=GodmchManageCallService.getAgentType(MchId, 1);
            Integer AgentId = GodmchManageCallService.getAgentMchId(MchId, agenttype);
            //添加导入商品
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(MchId, "", true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ImportAgentProductBean initImportAgentProduct = AddAgentProductCallService.initImportAgentProduct(AgentId, "", productId, MchId, false);
			String AgentProductId = AddAgentProductCallService.ImportAgentProduct(initImportAgentProduct);
			
			 //添加导入商品
			AddAgentProductBean addAgentProductBean2 = AddAgentProductCallService.initAddAgentProduct(MchId, "", true);
			addAgentProductBean2.setThirdCategoryId(1122);
			addAgentProductBean2.setCategoryId(1122);
			String productId2 = AddAgentProductCallService.AddAgentProduct(addAgentProductBean2);
			ImportAgentProductBean initImportAgentProduct2 = AddAgentProductCallService.initImportAgentProduct(AgentId, "", productId2, MchId, false);
			String AgentProductId2 = AddAgentProductCallService.ImportAgentProduct(initImportAgentProduct2);
			
			batchoffshelfordeleteproductBean.setUserId(MchId);
			batchoffshelfordeleteproductBean.setCategoryIdList(Arrays.asList(addAgentProductBean.getCategoryId()));
			batchoffshelfordeleteproductCall.setData(batchoffshelfordeleteproductBean);
			batchoffshelfordeleteproductCall.callService();			
			Logger.verifyEquals("200", batchoffshelfordeleteproductCall.getString("code"), "验证code");
			verifyMq(Arrays.asList(productId), MchId);
			
			
			List<ProductListDto> getProductLists = AddAgentProductCallService.GetProductLists(MchId);
			Logger.verifyEquals(1, getProductLists.size(), "验证上架商品");
			List<ProductListDto> getProductLists1 = AddAgentProductCallService.GetProductLists(AgentId);
			Logger.verifyEquals(1, getProductLists1.size(), "验证上架商品");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchOffShelfOrDeleteProduct_004() {
		Logger.start(true,"商户下架商品，代理商跟着移除.使用仓库id筛选");
		try {
			Integer MchId = GodmchManageCallService.CreateGodMch();
            Integer agenttype=GodmchManageCallService.getAgentType(MchId, 1);
            Integer AgentId = GodmchManageCallService.getAgentMchId(MchId, agenttype);
            //添加导入商品
            createDepotBean bean=GodmchManageCallService.createDepot(MchId);
			List<mch_depot_info> selectDepotByDepotTitle = GodmchManageCallService.SelectDepotByDepotTitle(bean.getDepotTitle());
			
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(MchId, "", true);
			addAgentProductBean.setDepotId(selectDepotByDepotTitle.get(0).getDepotId());
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ImportAgentProductBean initImportAgentProduct = AddAgentProductCallService.initImportAgentProduct(AgentId, "", productId, MchId, false);
			String AgentProductId = AddAgentProductCallService.ImportAgentProduct(initImportAgentProduct);
			
			 //添加导入商品
			AddAgentProductBean addAgentProductBean2 = AddAgentProductCallService.initAddAgentProduct(MchId, "", true);
			addAgentProductBean2.setDepotId(null);
			String productId2 = AddAgentProductCallService.AddAgentProduct(addAgentProductBean2);
			ImportAgentProductBean initImportAgentProduct2 = AddAgentProductCallService.initImportAgentProduct(AgentId, "", productId2, MchId, false);
			String AgentProductId2 = AddAgentProductCallService.ImportAgentProduct(initImportAgentProduct2);
			
			batchoffshelfordeleteproductBean.setUserId(MchId);
			batchoffshelfordeleteproductBean.setDepotIdList(Arrays.asList(addAgentProductBean.getDepotId()));
			batchoffshelfordeleteproductCall.setData(batchoffshelfordeleteproductBean);
			batchoffshelfordeleteproductCall.callService();			
			Logger.verifyEquals("200", batchoffshelfordeleteproductCall.getString("code"), "验证code");
			verifyMq(Arrays.asList(productId), MchId);
			
			
			List<ProductListDto> getProductLists = AddAgentProductCallService.GetProductLists(MchId);
			Logger.verifyEquals(1, getProductLists.size(), "验证上架商品");
			List<ProductListDto> getProductLists1 = AddAgentProductCallService.GetProductLists(AgentId);
			Logger.verifyEquals(1, getProductLists1.size(), "验证上架商品");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	
	
}